﻿#ifndef JZGL_CAMERA_H_
#define JZGL_CAMERA_H_

#include <QOpenGLWidget>
#include <QOpenGLFunctions>
#include <QOpenGLShaderProgram>
#include <QOpenGLBuffer>
#include <QOpenGLTexture>
#include <QImage>
#include <iostream>
#include <QVector3D>

class JZGLView;
class JZGLCamera 
{
public:
    enum RoateType{
        Yaw,   //偏航角, 头部左右旋转
        Pitch, //俯仰角, 头部向上或向下
        Roll,  //翻滚角, 翻滚角, 改变up
    }; 

    JZGLCamera();

    void setWidget(JZGLView* widget);
    JZGLView* widget();

    void setFovY(float fovY);
    void setViewport(int width, int height);

    QVector3D position();
    QVector3D lookPosition();
    void lookAt(QVector3D pos, QVector3D target, QVector3D up = QVector3D(0.0f, 1.0f, 0.0f)); //up是本地坐标

    float farPlane();
    void setFarPlane(float far_plane);

    float nearPlane();
    void setNearPlane(float near_plane);

    QMatrix4x4 getViewMatrix();
    QMatrix4x4 getProjectionMatrix();
    QMatrix4x4 getWindowMatrix();

    void setRotate(float x, float y, float z);
    void rotate(float &x, float& y, float &z);

    QPoint project(float x, float y, float z);
    QVector3D unProject(int x,int y,float depth);  //x,y 屏幕坐标系， 这里的depth 是readpixel 读出来的非线性深度值

    void lookAtRotate(RoateType type, float angle);  //绕观察点旋转, 向上
    void rotate(float yaw, float pitch, float roll);

private:
    void update();
    QVector3D getWorldUp();

    QVector3D m_eye;    //世界坐标
    QVector3D m_center; //世界坐标
    QVector3D m_up;     //本地坐标
    
    int m_width;
    int m_height;
    float m_far;
    float m_near;
    float m_fovY;

    JZGLView* m_widget;
};


#endif // !CAMERA_H_
